﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using bzxxxt.bll;
using bzxxxt.dal;
using bzxxxt.admin.service;
using bzxxxt.admin.entity;
using bzxxxt.model;

namespace bzxxxt.admin.ashx
{
    /// <summary>
    /// bg_dead 的摘要说明
    /// </summary>
    public class bg_dead : admin.ashx.rm_base
    {
        DeadBLL deadBLL = new DeadBLL();

        public override void add(HttpContext context)
        {
            string name = context.Request.Params["ui_dead_name"] ?? "";
            string cardType = context.Request.Params["ui_dead_cardtype"] ?? "";
            string cardno = context.Request.Params["ui_dead_cardno"] ?? "";
            string gender = context.Request.Params["ui_dead_gender"] ?? "";
            string firetime = context.Request.Params["ui_dead_firetime"] ?? "";
            string deathtime = context.Request.Params["ui_dead_deathtime"] ?? "";
            string nation = context.Request.Params["ui_dead_nation"] ?? "";
            string city = context.Request.Params["ui_dead_city"] ?? "";
            string areacode = context.Request.Params["ui_dead_areaId"] ?? "";
            string profession = context.Request.Params["ui_dead_profession"] ?? "";
            string education = context.Request.Params["ui_dead_education"] ?? "";
            string deathcause = context.Request.Params["ui_dead_deathcause"] ?? "";
            string deathplace = context.Request.Params["ui_dead_deathplace"] ?? "";
            string birthAddress = context.Request.Params["ui_dead_birAddress"] ?? "";
            string homeAddress = context.Request.Params["ui_dead_homeAddress"] ?? "";
            string photo = context.Request.Params["ui_dead_photo"] ?? "";
            string bonetype = context.Request.Params["ui_dead_bonetype"] ?? "";

            string fileName = context.Server.MapPath(photo);
            string photo2 = "/admin/images/" + cardno + Path.GetExtension(fileName);
            if (photo.IndexOf("dead.jpg") < 0 && photo != photo2)
            {
                //修改了图片
                string photoFile = context.Server.MapPath("/") + photo2;
                if (File.Exists(photoFile))
                {
                    File.Delete(photoFile);
                }
                File.Move(fileName, photoFile);
            }
            else
            {
                photo2 = photo;
            }            
            string estTime = context.Request.Params["ui_business_estTime"] ?? "";

            tbDead dead = new tbDead
            {
                name = name,                
                cardno = cardno,
                gender = gender,
                actFiretime = CommUtil.ToDateTime(firetime),
                deathtime = CommUtil.ToDateTime(deathtime),
                cardtype = cardType,
                educationid = education,
                professionid = profession,
                deathcaurseid = deathcause,
                deathplace = deathplace,
                nationid = nation,
                areacode = areacode,
                birthAddress = birthAddress,
                homeAddress = homeAddress,
                addtime = DateTime.Now,
                photo = photo2
            };           
            context.Response.Write(deadBLL.AddDead(dead));
        }

        public override void edit(HttpContext context)
        {
            string id = context.Request.Params["ui_dead_id"] ?? "";
            string name = context.Request.Params["ui_dead_name"] ?? "";
            string cardType = context.Request.Params["ui_dead_cardtype"] ?? "";
            string cardno = context.Request.Params["ui_dead_cardno"] ?? "";
            string gender = context.Request.Params["ui_dead_gender"] ?? "";
            string firetime = context.Request.Params["ui_dead_firetime"] ?? "";
            string deathtime = context.Request.Params["ui_dead_deathtime"] ?? "";
            string nation = context.Request.Params["ui_dead_nation"] ?? "";
            string areacode = context.Request.Params["ui_dead_areaId"] ?? "";
            string profession = context.Request.Params["ui_dead_profession"] ?? "";
            string education = context.Request.Params["ui_dead_education"] ?? "";
            string deathcause = context.Request.Params["ui_dead_deathcause"] ?? "";
            string deathplace = context.Request.Params["ui_dead_deathplace"] ?? "";
            string birthAddress = context.Request.Params["ui_dead_birAddress"] ?? "";
            string homeAddress = context.Request.Params["ui_dead_homeAddress"] ?? "";
            string photo = context.Request.Params["ui_dead_photo"] ?? "";
            string bonetype = context.Request.Params["ui_dead_bonetype"] ?? "";


            string relativeId = context.Request.Params["ui_relative_id"];
            if (string.IsNullOrEmpty(relativeId)) relativeId = "0";            
            //string estArriveTime = context.Request.Params["ui_dead_estArrivetime"] ?? "";
            //string estArrivePlace = context.Request.Params["ui_dead_estArriveplace"] ?? "";

            string fileName = context.Server.MapPath(photo);
            string photo2 = "/admin/images/" + cardno + Path.GetExtension(fileName);
            if (photo.IndexOf("dead.jpg") < 0 && photo != photo2)
            {
                //修改了图片
                string photoFile = context.Server.MapPath("/") + photo2;
                if (File.Exists(photoFile))
                {
                    File.Delete(photoFile);
                }
                File.Move(fileName, photoFile);
            }
            else
            {
                photo2 = photo;
            }
            tbDead dead = new tbDead
            {
                id = int.Parse(id),
                name = name,
                cardno = cardno,
                gender = gender,
                actFiretime = CommUtil.ToDateTime(firetime),
                deathtime = CommUtil.ToDateTime(deathtime),
                cardtype = cardType,
                educationid = education,
                professionid = profession,
                deathcaurseid = deathcause,
                deathplace = deathplace,
                nationid = nation,
                areacode = areacode,
                birthAddress = birthAddress,
                homeAddress = homeAddress,                
                //estArrivetime = DateTime.Parse(estArriveTime),
                //estArriveplace = estArrivePlace,
                photo = photo2
            };

            context.Response.Write(deadBLL.EditDead(dead));     
        }

        public override void delete(HttpContext context)
        {
            string ids = context.Request.Params["id"];
            context.Response.Write(deadBLL.DelDead(ids));      
        }

        public override void getall(HttpContext context)
        {
            string where = context.Request.Params["where"] ?? "1=1";
            int pageIndex = CommUtil.GetPageIndex(context);
            int pageSize = CommUtil.GetPageSize(context);
            string mySort = CommUtil.GetSort(context) + " " + CommUtil.GetOrder(context);

            context.Response.Write(deadBLL.GetListByPage(pageSize, pageIndex, mySort, where,"1")); 
        }

        public override void search(HttpContext context)
        {
            string where = context.Request.Params["where"] ?? "1=1";
            string flag = context.Request.Params["flag"] ?? "0";

            int pageIndex = CommUtil.GetPageIndex(context);
            int pageSize = CommUtil.GetPageSize(context);
            string mySort = CommUtil.GetSort(context) + " " + CommUtil.GetOrder(context);

            context.Response.Write(deadBLL.GetListByPage(pageSize, pageIndex, mySort, where,flag)); 
        }
        public override void other(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request.Params["action"];

                switch (action)
                {
                    case "getDeadByName":
                        {
                            string name = context.Request.Params["name"] ?? "";
                            string json = deadBLL.getDeadRelativeByName(name);
                            context.Response.Write( json);
                        
                            break;
                        }
                    case "getDead":
                        {
                            string cardno = context.Request.Params["cardno"] ?? "0";
                            context.Response.Write(deadBLL.getDeadRelative(cardno)); 
                            break;
                        }                    
                    case "searchDead":
                        string graveId = context.Request.Params["graveId"] ?? "0";
                        context.Response.Write(deadBLL.searchDead(int.Parse(graveId)));        
                        break;
                    case "reserve":
                        {
                            string estArriveplace = context.Request.Params["ui_dead_estArriveplace"];
                            string estArrivetime = context.Request.Params["ui_dead_estArrivetime"];
                            string deadId = context.Request.Params["ui_dead_id"];
                            
                            string relativeId = context.Request.Params["ui_relative_id"];
                            if (string.IsNullOrEmpty(relativeId)) relativeId = "0";
                            int rid = int.Parse(relativeId);
                            string relation = context.Request.Params["ui_relative_relation"];
                            tbDead d = deadBLL.getDead(int.Parse(deadId));
                            if (rid < 1) {
                                context.Response.Write(JsonUtil.ToJson(new { success = false,msg="当前联系人不存在，请先添加择联系人！"}));
                                return;
                            }
                            d.relativeid = rid;
                            d.relationid = relation;
                            d.estArriveplace = estArriveplace;
                            d.estArrivetime = DateTime.Parse(estArrivetime);
                            context.Response.Write(deadBLL.savedead(d));
                            break;
                        }
                    case "reserve2":
                        {
                            string paradisecode = context.Request.Params["ui_dead_estStoreplace"];
                            string estStoretime = context.Request.Params["ui_dead_estStoretime"];
                            string firetime = context.Request.Params["ui_dead_firetime"];
                            string deadId = context.Request.Params["ui_dead_id"];

                            string relativeId = context.Request.Params["ui_relative_id"];
                            if (string.IsNullOrEmpty(relativeId)) relativeId = "0";
                            int rid = int.Parse(relativeId);
                            string relation = context.Request.Params["ui_relative_relation"];
                            tbDead d = deadBLL.getDead(int.Parse(deadId));
                            if (rid < 1)
                            {
                                context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "当前联系人不存在，请先添加择联系人！" }));
                                return;
                            }
                            d.relativeid = rid;
                            d.relationid = relation;
                            d.estStoreplace = deadBLL.getParadiseName( paradisecode);
                            d.actFiretime = DateTime.Parse(firetime);
                            d.estStoretime = DateTime.Parse(estStoretime);
                            context.Response.Write(deadBLL.savedead(d));
                            break;
                        }
                    case "import":
                        {
                            string op = context.Request.Params["op"];
                            if (op == "upload")
                            {
                                Cfg.IsRunning = false;
                                UserEntity user = UserService.GetUser(context);
                                //文件名  
                                HttpPostedFile f1 = context.Request.Files["ui_dead_import_file"];

                                string fileExt = System.IO.Path.GetExtension(f1.FileName);
                                if (!(fileExt == ".xls" || fileExt == ".xlsx"))
                                {
                                    context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "只支持扩展名为.xls或.xlsx文件！" }));
                                    return;
                                }
                                string fileName = user.userName + "_dead_" + DateTime.Now.ToString("yyyyMMddHHmmss") + fileExt;
                                f1.SaveAs(context.Server.MapPath("~/admin/upload/") + fileName);
                                context.Response.Write(JsonUtil.ToJson(new { success = true, msg = fileName }));
                            }
                            else if (op == "import")
                            {

                                string file = context.Request.Params["ui_dead_import_filename"];
                               
                                if (string.IsNullOrEmpty(file))
                                {
                                    context.Response.Write(JsonUtil.ToJson(new { success = false, msg = "文件未上传！" }));
                                }
                                else
                                {
                                    file = context.Server.MapPath("~/admin/upload/") + file;
                                    try
                                    {
                                        Cfg.IsRunning = true;
                                        deadBLL.ParseDeadFile(file, 0, 1);
                                        context.Response.Write(JsonUtil.ToJson(new { success = true, msg = "导入成功！" }));
                                    }
                                    catch (Exception ex)
                                    {
                                        //string json = JsonUtil.ToJson(new { success = false, msg = "/admin/upload/log.html" });
                                        string json = JsonUtil.ToJson(new { success = false, msg = ex.Message });
                                        context.Response.Write(json);
                                    }
                                    finally
                                    {
                                        File.Delete(file);
                                    }
                                }
                                
                            }
                            break;
                        }
                }
            
        }

    }
}